import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/users.vue'
import Roles from '../views/roles.vue'
import Rights from '../views/rights.vue'
import Goods from '../views/goods.vue'
import Params from '../views/params.vue'
import Categories from '../views/categories.vue'
import Orders from '../views/orders.vue'
import Reports from '../views/reports.vue'

import Add from '../views/Add.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'Login', component: Login },
  {
    path: '/home', name: 'Home', component: Home, redirect: '/welcome', children: [
    { path: '/welcome', name: 'Welcome', component: Welcome }, 
    { path: '/users', name: 'Users', component: Users }, 
    { path: '/roles', name: 'Roles', component: Roles }, 
    { path: '/rights', name: 'Rights', component: Rights }, 
    { path: '/goods', name: 'Goods', component: Goods},
    {path:'/goods/add',component:Add},
    { path: '/params', name: 'Params', component: Params },
    { path: '/categories', name: 'Categories', component: Categories },
    { path: '/orders', name: 'Orders', component: Orders },
    { path: '/reports', name: 'Reports', component: Reports }

    ]
  },

]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login');
  next();
})

export default router
